import {createRouter, createWebHashHistory} from "vue-router"
import {isLogin, isLogout, changeMenuSelected} from '@router'
import {RouteNameConst} from '@config'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      name: RouteNameConst.index.en,
      path: '/',
      component: () => import('@/pages/index/index.vue'),
      // children: [
      //   {
      //     name: RouteNameConst.sys.en,
      //     path: 'sys',
      //     children: [
      //       {
      //         name: RouteNameConst.sys.children.menu.en,
      //         path:'menu',
      //         component: () => import('@sys/menu/pages/sys-menu.vue')
      //       },
      //       {
      //         name: RouteNameConst.sys.children.user.en,
      //         path: 'user',
      //         component: () => import('@sys/user/pages/sys-user.vue')
      //       }
      //     ]
      //   }
      // ]
    },
    {
      name: RouteNameConst.login.en,
      path: '/login',
      component: () => import('@login/page/login.vue'),
    }
  ]
})

// 全局路由前置守卫
router.beforeEach(async (to, from, next) => {
  // 如果在判断函数中判断结果为不能前往要访问的路由，则会返回 false，防止其从函数返回后在当前函数中执行 next
  const res = await isLogin(to, next) && await isLogout(to, next)
  if (res) next()
})

// 全局路由后置守卫
router.afterEach(() => {
  // 修改选中的菜单项
  changeMenuSelected()
})

export default router
